#!/bin/bash

set -e

rmroots() {
	echo "Cleaning $tname ($1)"

	mount -t cgroup none $tname -o "$1"

	for d in "$tname/oldroot" "$tname/newroot" "$tname/zdtmtstroot"; do
		test -d "$d" && rmdir "$d"
	done

	echo "Left there is:"
	ls "$tname"
	umount "$tname"
}

tname=$(mktemp -d cgclean.XXXXXX)

for ctl in $(cat /proc/self/cgroup | cut -d: -f2); do
	rmroots "$ctl"
done

rmdir $tname
